import pygame
import random
import math
import time
# Initialize pygame
pygame.init()
# Constants
GRAPH_WIDTH = 1500
GRAPH_HEIGHT = 800
BK_COLOR = (255, 255, 255)  # White background
FILL_COLOR = (255, 0, 0)    # Red color for plants
LINE_COLOR = (0, 0, 0)      # Black color for text and lines
TEXT_COLOR = (255, 255, 0)  # Yellow color for text
TEXT_HEIGHT = 50
TEXT_WIDTH = 20
ENEMY_MAX = 10
ENEMY_LIFE=60
FIRE_IMG_PATH = "fire.png"              # Fire image

# Game classes
class Plant:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.w = 100
        self.h = 100
        self.speed=10

    def move(self):
        keys = pygame.key.get_pressed()  # 获取所有按键的状态
        if keys[pygame.K_a]:  # 如果按下A键
            self.x -= self.speed
        if keys[pygame.K_d]:  # 如果按下D键
            self.x += self.speed
        if keys[pygame.K_w]:  # 如果按下W键
            self.y -= self.speed
        if keys[pygame.K_s]:  # 如果按下S键
            self.y += self.speed

    def put_img(self, screen):
        PLANT_IMG_PATH = f"Imitater_spin{int(time.time()*20) % 6 + 1}.png"
        self.image = pygame.image.load(PLANT_IMG_PATH)  # Load plant image
        self.image = pygame.transform.scale(self.image, (self.w, self.h))
        screen.blit(self.image, (self.x, self.y))

    def get_x(self):
        return self.x

    def get_y(self):
        return self.y

class Fire:
    def __init__(self, w, h, radim, angle):
        self.w = w
        self.h = h
        self.radim = radim
        self.angle=angle
    def move(self, plant):
        self.angle += 0.2
        self.x = plant.get_x() + self.radim * math.cos(self.angle)
        self.y = plant.get_y() + self.radim * math.sin(self.angle)
    def put_img(self,screen):
        self.image = pygame.image.load(FIRE_IMG_PATH)  # Load fire image
        self.image = pygame.transform.scale(self.image, (self.w, self.h))
        screen.blit(self.image,(self.x,self.y))
    def get_x(self):
        return self.x

    def get_y(self):
        return self.y

class Enemy:
    def __init__(self, plant, speed, radim):
        rand=random.uniform(0,2*math.pi)
        self.x = plant.get_x() +math.cos(rand)*radim
        self.y = plant.get_y() +math.sin(rand)*radim
        self.speed = speed
        self.radim = radim
        self.life = 0

    def move(self, plant):
        self.angle = math.atan2((plant.y - self.y) , (plant.x - self.x))
        self.x += self.speed * math.cos(self.angle)
        self.y += self.speed * math.sin(self.angle)
        self.w=100
        self.h=100

    def put_img(self,scren):
        ENEMY_IMG_PATH = f"enemy{int(time.time()*12) % 10 + 1}.png"
        self.image = pygame.image.load(ENEMY_IMG_PATH)  # Load plant image
        self.image = pygame.transform.scale(self.image, (self.w, self.h))
        screen.blit(self.image, (self.x, self.y))

    def hit(self, plant):
        # Check if enemy hits the plant
        # Get plant's bounding box (left, right, top, bottom)
        plant_left = plant.x
        plant_right = plant.x + plant.image.get_width()
        plant_top = plant.y
        plant_bottom = plant.y + plant.image.get_height()

        # Get enemy's bounding box (left, right, top, bottom)
        enemy_left = self.x
        enemy_right = self.x + self.image.get_width()
        enemy_top = self.y
        enemy_bottom = self.y + self.image.get_height()

        # Check if bounding boxes overlap
        return (plant_right >= enemy_left and plant_left <= enemy_right and
                plant_bottom >= enemy_top and plant_top <= enemy_bottom)


    def hit_fire(self, fire):
    # Check if enemy hits fire
    # Get fire's bounding box (left, right, top, bottom)
      fire_left = fire.x
      fire_right = fire.x + fire.image.get_width()
      fire_top = fire.y
      fire_bottom = fire.y + fire.image.get_height()

    # Get enemy's bounding box (left, right, top, bottom)
      enemy_left = self.x
      enemy_right = self.x + self.image.get_width()
      enemy_top = self.y
      enemy_bottom = self.y + self.image.get_height()

    # Check if bounding boxes overlap
      return (fire_right >= enemy_left and fire_left <= enemy_right and
            fire_bottom >= enemy_top and fire_top <= enemy_bottom)


# Game functions
def game_exit():
    pygame.quit()
    quit()

def text_scan(screen, text, x, y, value):
    font = pygame.font.SysFont("Arial", TEXT_HEIGHT)
    text_surface = font.render(f"{text}: {value}", True, TEXT_COLOR)
    screen.blit(text_surface, (x, y))

# Main game loop
# Initialize pygame window
screen = pygame.display.set_mode((GRAPH_WIDTH, GRAPH_HEIGHT))
pygame.display.set_caption("Game")
pygame.mixer.music.load("2058939215.mp3")
pygame.mixer.music.play(100,30)
plant_obj = Plant(700, 350)
fire1 = Fire(100, 50, 200, 0)
fire2 = Fire(100, 50, 200, 2 * math.pi / 3)
fire3 = Fire(100, 50, 200, 4 * math.pi / 3)
vector_enemy = [Enemy(plant_obj, 1, 300) for _ in range(ENEMY_MAX)]

clock = pygame.time.Clock()
gold = 0
count = 0
clock = pygame.time.Clock()
running = True
while running:
        screen.fill(BK_COLOR)
        BACKGROUND_IMG_PATH = f"bk{int(time.time()*12) % 10 + 1}.png"
        background_img = pygame.image.load(BACKGROUND_IMG_PATH)
        background_img = pygame.transform.scale(background_img, (GRAPH_WIDTH, GRAPH_HEIGHT + 200))
        # Draw the background
        screen.blit(background_img, (0, -100))
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            plant_obj.move()
        plant_obj.put_img(screen)

        # Fire movement
        fire1.move(plant_obj)
        fire2.move(plant_obj)
        fire3.move(plant_obj)
        fire1.put_img(screen)
        fire2.put_img(screen)
        fire3.put_img(screen)
        for enemy in vector_enemy:
            if enemy.life==0 and count % 60 == 0:
                enemy.life = ENEMY_LIFE
            if enemy.life>0:
                enemy.move(plant_obj)
                enemy.put_img(screen)
                if enemy.hit(plant_obj):
                    game_exit()
                if enemy.hit_fire(fire1) or enemy.hit_fire(fire2) or enemy.hit_fire(fire3):
                    enemy.life -=1
                    if enemy.life==0:
                       rand = random.uniform(0,2*math.pi)
                       enemy.x = plant_obj.get_x() + math.cos(rand) * enemy.radim
                       enemy.y = plant_obj.get_y() + math.sin(rand) * enemy.radim
                       gold += 1

        text_scan(screen, "击杀僵尸数量：", 100, 100, gold)
        pygame.display.update()
        count += 1
        clock.tick(60)



